package gui;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import simulation.initiatives.AbstractEngine;

import common.Constants;
import common.IGrid;
import common.SimulationSettings;

public class EarthPanel extends JPanel {

  private static final long serialVersionUID = -1108120537851962997L;  
  private SunDisplay sunDisplay;
  private EarthGrid earth;
  
  public EarthPanel(SimulationSettings settings) {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setMinimumSize(new Dimension(20,20));
    setMaximumSize(new Dimension(Constants.FRAME_WIDTH,Constants.FRAME_HEIGHT));
    setPreferredSize(new Dimension(20, 20));
    
    if(settings.isPresentationThreaded()) {
      Thread t;
      if(settings.getInitiative().equals(Constants.INITIATIVE_PULL)) {
        earth = new EarthGridThreadPull();
        t = new Thread(earth);
        t.setName("PresThread-Pulling");
      }
      else {
        earth = new EarthGridThreadPush();
        t = new Thread(earth);
        t.setName("PresThread-Pushing");
      }
      t.start();
    }
    else {
      earth = new EarthGrid();
    }
    
    earth.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    settings.setRadius(getRadius());
    
    sunDisplay = new SunDisplay(earth.getWidth());
    sunDisplay.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    add(sunDisplay);
    add(earth);
  }
  
  public void drawGrid(int degreeSeparation) {
    earth.setGridSize(degreeSeparation);
    sunDisplay.drawSunPath(earth.getWidth());
    repaint();
  }
  
  public int getRadius() {
    return earth.getRadius();
  }
  
  public void updateGrid(AbstractEngine engine) {
    earth.updateGrid(engine);
  }
  
  public void updateGrid(IGrid grid) {
    earth.updateGrid(grid);
  }
  
  public void moveSunPosition(float degrees) {
    sunDisplay.moveSunPosition(degrees);
    repaint();
  }
  
  public void reset() {
    sunDisplay.reset();
    earth.reset();
    repaint();
  }
  
  public void setMapOpacity(float value) {
    earth.setMapOpacity(value);
    repaint();
  }
  
  public void enableTooltips() {
    earth.enableTooltips();
  }
  
  public void disableTooltips() {
    earth.disableTooltips();
  }

}
